package push;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import push.android.AndroidBroadcast;

import baas.Util;

import dbbaas.DBServlet;
import dbbaas.DBUtil;
import dbbaas.ResMsgItem;

public class PushServlet extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8909426799651129659L;
	private String title;
	private String content;
	private String user_code;
	private String sql;
	private String device_token;
	private PushClient client = new PushClient();
	private String appkey = "566e1c8867e58e49ad0012f0";
	private String appMasterSecret = "mskxamo3nxzimwpkkjymlnh1shxeez9k";
	
	public PushServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		title = request.getParameter("title");
		title = URLDecoder.decode(title, "utf-8");
		content = request.getParameter("content");
		content = URLDecoder.decode(title, "utf-8");
		user_code = request.getParameter("user_code");
		ResMsgItem resMsgItem = new ResMsgItem();
		resMsgItem.setSum(0);
		resMsgItem.setCode(0);
		sql = "select device_token from users where user_code=" + "\'"
				+ user_code + "\'";
		Connection conn = DBUtil.getDBConnection("jwtask");
		try {
			PreparedStatement pstm1 = conn.prepareStatement(sql);
			ResultSet rs1 = pstm1.executeQuery();
			if (rs1.next()) {
				device_token=rs1.getString("device_token");
			} else {
				resMsgItem.setCode(1);
				resMsgItem.setMessage("error");
				resMsgItem.setMessage("该用户尚未获取到device_token！");
				Util.writeJsonToResponse(response, resMsgItem);// 在页面输出信息
			}
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(device_token!=null&&!device_token.equals("")){	
			PushItem pushItem = new PushItem();
			pushItem.setTitle(title);
			pushItem.setContent(content);
			pushItem.setDevice_token(device_token);
			// TODO set your appkey and master secret here
			PhshManager phshManager = new PhshManager("566e1c8867e58e49ad0012f0", "mskxamo3nxzimwpkkjymlnh1shxeez9k");
			
							try {
					phshManager.sendAndroidUnicast(pushItem);
				} catch (Exception e) {
					e.printStackTrace();
				}		
		}
	}

	public void init() throws ServletException {
		// Put your code here
	}
	
	public void sendAndroidBroadcast(PushItem pushItem) throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
		broadcast.setTicker( "Android broadcast ticker");
		broadcast.setTitle(pushItem.getTitle());
		broadcast.setText(pushItem.getContent());
		broadcast.goActivityAfterOpen("com.jeeweel.syl.jwtask.business.main.tab.TabHostActivity");
		broadcast.setBuilderId(1);
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		broadcast.setProductionMode();
		// Set customized fields
		broadcast.setExtraField("test", "helloworld");
		client.send(broadcast);
	}

}
